package org.springblade.modules.repair.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.springblade.common.cache.DictCache;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.repair.vo.TemplateVO;
import org.springblade.modules.system.entity.Dict;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.repair.mapper.MaintainTemplateMapper;
import org.springblade.modules.repair.entity.MaintainTemplate;
import org.springblade.modules.repair.service.MaintainTemplateService;

@Service
public class MaintainTemplateServiceImpl extends BaseServiceImpl<MaintainTemplateMapper, MaintainTemplate> implements MaintainTemplateService {

	@Override
	public List<TemplateVO> listByElevatorUseType(String code, String tenantId) {
		List<String> tenantIds = new ArrayList<>();
		tenantIds.add(tenantId);
		tenantIds.add("000000");
		List<Dict> userType = DictCache.getList("apply_elevator_type");
		List<TemplateVO> result = userType.stream().map(e -> {
			TemplateVO templateVO = new TemplateVO();
			List<TemplateVO> templateVOS = baseMapper.listByElevatorUseType(e.getDictKey(), tenantIds);
			templateVO.setId(e.getDictKey());
			templateVO.setName(e.getDictValue());
			templateVO.setParentId("0");
			templateVO.setChildren(templateVOS);
			return templateVO;
		}).filter(e -> ObjectUtil.isNotEmpty(e.getChildren())).collect(Collectors.toList());
		return result;
	}

	@Override
	public List<TemplateVO> listByElevatorUseTypeV2(String code, String tenantId) {
		List<String> tenantIds = new ArrayList<>();
		tenantIds.add(tenantId);
		tenantIds.add("000000");
		List<TemplateVO> list = baseMapper.listByElevatorUseTypeV2(code, tenantIds);
		for (TemplateVO templateVO : list) {
			templateVO.setParentId("0");
			List<TemplateVO> children = templateVO.getChildren();
			for (TemplateVO templateVO1 : children) {
				templateVO1.setParentId(templateVO.getId());
			}
			templateVO.setChildren(children);
		}
		return list;
	}
}

